package com.hxkcloud.nurse.activitys;

import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.PushRecordAdapter;
import com.hxkcloud.nurse.beans.PushBean;
import com.hxkcloud.nurse.beans.PushRecordBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 推送记录
 */
public class PushRecordActivity extends BaseActivity implements View.OnClickListener, PullToRefreshLayout.OnRefreshListener {
    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_del;
    private String title = "推送记录";

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;
    private PushRecordAdapter pushRecordAdapter;
    private List<PushRecordBean> pushRecordBeanList;
    private int pageIndex = 1;
    private int pageSize = 15;
    private String scaleId = null;
    private String questionId = null;
    private PushBean resultBean;
    private int totalCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push_record);
        title = this.getIntent().getStringExtra("title");
        if (getIntent().hasExtra("scaleId")) {
            scaleId = this.getIntent().getStringExtra("scaleId");
        }
        if (getIntent().hasExtra("questionId")) {
            questionId = this.getIntent().getStringExtra("questionId");
        }

        initView();
        initData();
        initListener();
    }

    @Override
    protected void updateByHandler(Message msg) {

        if (pageIndex == 1) {
            refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
        } else {
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }
        switch (msg.what) {
            case MessageCode.PUSH_SUCCESS:
                if (msg.obj != null) {
                    resultBean = (PushBean) msg.obj;
                    totalCount = Integer.parseInt(resultBean.getTotalCount());
                    if (resultBean.getList().size() == 0) {
                        toastMsg("没有更多了");
                    } else {
                        if (pageIndex == 1) {
                            pushRecordBeanList.clear();
                        }
                        pushRecordBeanList.addAll(resultBean.getList());
                        pushRecordAdapter.notifyDataSetChanged();
                    }
                }

                break;
            case MessageCode.PUSH_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }
                break;
            case MessageCode.NETWORKCODE_CONNECT_ERROR:
                toastMsg("网络错误!");
                break;


        }
        dismisssLoading();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_del = (TextView) findViewById(R.id.tv_title_func);
        pushRecordBeanList = new ArrayList<>();
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refreshlayout_assessrecordlist);
        recycleView = (PullableRecyclerView) findViewById(R.id.recyclerview_assessrecordlist);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(title);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        pushRecordAdapter = new PushRecordAdapter(pushRecordBeanList, this);
        recycleView.setAdapter(pushRecordAdapter);
        // recycleView.
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData() {
        loading();
        if (scaleId != null)
            YiJiaHuNetWorkUtils.getPushRecord(scaleId, pageIndex, pageSize, handler);
        else
            YiJiaHuNetWorkUtils.getPushQuestionRecord(questionId, pageIndex, pageSize, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        pageIndex = 1;
        initData();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        if (pushRecordBeanList.size() == totalCount) {
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            return;
        }
        pageIndex++;
        initData();
    }
}
